<?php
namespace plugins\member\controller;

use cmf\controller\PluginAdminBaseController;
use RongCloud\Lib\Utils;
use RongCloud\RongCloud;
use think\Db;

define("APPKEY", 'p5tvi9dspe834');
define('APPSECRET', 'b8fHKCCpFMNe0N');

class ImController extends PluginAdminBaseController
{
	protected function _initialize()
	{

	}

	/**
	*	IM首页
	*/
	public function im()
	{
        $info = Db::name('user')->where('employee_id','10001001')->find();
        if(!empty($info) && empty($info['token'])){
            $this->register();
        }
		return $this->fetch();
	}

    /**
     * 用户注册
     */
    function register() {
        $path = dirname(__FILE__);
        $path = str_replace("member","member_login",$path);
        require $path.'/RongCloud/RongCloud.php';
        $RongSDK = new RongCloud(APPKEY, APPSECRET);
        $user = [
            'id' => '10001001',
            'name' => 'admin',//用户名称
            'portrait' => ZY_APP_PATH.'/plugins/member/view/public/kf.png' //用户头像
        ];
        $register = $RongSDK->getUser()->register($user);
        if($register['code'] == 200){
            Db::name('user')->where('employee_id','10001001')->update([
                'token'=>$register['token']
            ]);
        }
    }


}
